<?php
// +----------------------------------------------------------------------
// | UCToo [ Universal Convergence Technology ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2017 http://uctoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Patrick <contact@uctoo.com>
// +----------------------------------------------------------------------
namespace app\mpbase\controller;

use com\TPWechat;
use com\wxpay\WxPayApi;
use com\wxpay\database\WxPayNotify;
use com\wxpay\database\WxPayOrderQuery;

class PayNotifyCallBack extends WxPayNotify
{
	//查询订单
	public function Queryorder($transaction_id)
	{
		$input = new WxPayOrderQuery();
		$input->setTransactionId($transaction_id);
		$result = WxPayApi::orderQuery($input);

		if(array_key_exists("return_code", $result)
			&& array_key_exists("result_code", $result)
			&& $result["return_code"] == "SUCCESS"
			&& $result["result_code"] == "SUCCESS")
		{
			return true;
		}
		return false;
	}
	
	//重写回调处理方法，成功的时候返回true，失败返回false，处理商城订单
	public function NotifyProcess($data, &$msg)
	{

		if(!array_key_exists("transaction_id", $data)){
			$msg = "输入参数不正确";
			return false;
		}
		//查询订单，判断订单真实性
		if(!$this->Queryorder($data["transaction_id"])){
			$msg = "订单查询失败";
			return false;
		}

        //以上的代码都是相同的，以下代码写定制业务逻辑，这里实现一个通用订单处理逻辑
        $transaction = model('transaction'); // 保存微信支付订单流水
        trace($data,'NotifyProcess');
        trace($msg,'NotifyProcessmsg');
        $transaction->data($data);
        $transaction->save();

        $map["out_trade_no"] = $data["out_trade_no"];
        $omap["order_id"] = $data["out_trade_no"];
        trace($omap,'omap111');
        $data["paySta"] = 1;
        $order = model('order');
        $order-> where($omap)->setField(["trans_id"=>$data["transaction_id"],'order_status'=>1]); //支付流水号写入订单
         //核销订单中使用的优惠券，核销逻辑并未提供完整演示，聪明的开发者请自己写 ：）
        if(empty($order['coupon'])){               //订单没有使用优惠券
            return true;
        }
		return true;
	}
}

